package yxy.game.pm2.command.resonate;

import yxy.cherry.game.base.message.MessageEmitterHandler;
import yxy.cherry.game.base.message.annotation.MessageSource;
import yxy.cherry.game.base.message.annotation.MessageType;
import yxy.game.pm2.Result;
import yxy.game.pm2.command.Command;
import yxy.game.pm2.message.ResonateMessage;
import yxy.game.pm2.module.resonate.ResonateHandle;
import yxy.game.pm2.module.resonate.ResonateModule;

import java.util.ArrayList;

/**
 * @author ljb
 * @name CMD_ResonateInfo
 * @descrition 共鸣系统页面
 * @since 2022-07-27
 */
@MessageType(proto = Command.ResonateInfo, classtype = ResonateMessage.ResonateInfoRequest.class)
public class CMD_ResonateInfo extends Command<ResonateMessage.ResonateInfoRequest> {

    @Override
    public void execute(MessageSource messageSource, ResonateMessage.ResonateInfoRequest request) {
        String playerCode = messageSource.getTag();

        ResonateMessage.ResonateInfoResponse.Builder response = ResonateMessage.ResonateInfoResponse.newBuilder();

        try {
            ResonateModule module = ResonateModule.get(playerCode);

            if (module == null) {
                send(messageSource, response, Result.DataError);
                return;
            }

            //todo 上面位置的武将返回（阵上的）
            ArrayList<ResonateMessage.HeroMsg> aboveHeroList = ResonateHandle.getAboveHeroList(module);
            response.addAllAboveHeros(aboveHeroList);

            //todo 下面位置的武将返回（共鸣的）
            ArrayList<ResonateMessage.HeroMsg> resonateHeros = ResonateHandle.getResonateHeros(module);
            response.addAllResonateHeros(resonateHeros);

            send(messageSource, response, Result.Success);
        } catch (Exception e) {
            e.printStackTrace();
            send(messageSource, response, Result.DataError);
        }
    }

    private void send(MessageSource messageSource, ResonateMessage.ResonateInfoResponse.Builder response, Result result) {
        response.setResult(result.packResult());
        MessageEmitterHandler.getInstance().send(messageSource, response.build());
    }
}